package com.kazarus.examplx00004;

import android.app.Activity;
import android.os.Bundle;

import android.widget.Button;
import android.widget.ListView;

import android.view.View;
import android.view.View.OnClickListener;

import com.kazarus.db.DataBaseHelper;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.widget.SimpleAdapter;

import java.util.*;



public class examplx00004 extends Activity 
{
	public Button Btnx_Create;
	public Button Btnx_Upgrad;
	public Button Btnx_Insert;
	public Button Btnx_Update;
	public Button Btnx_RefxIt;
	public ListView List_View;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        SetInitialize();    
    }
    
    public void SetInitialize()
    {
    	Btnx_Create = (Button)this.findViewById(R.id.Btnx_Create);
    	Btnx_Upgrad = (Button)this.findViewById(R.id.Btnx_Upgrad);
    	Btnx_Insert = (Button)this.findViewById(R.id.Btnx_Insert);
    	Btnx_Update = (Button)this.findViewById(R.id.Btnx_Update);
    	Btnx_RefxIt = (Button)this.findViewById(R.id.Btnx_RefxIt);    
    	List_View   = (ListView)this.findViewById(R.id.List_View);
    	
    	Btnx_Create.setOnClickListener(new OnBtnxCreateClickListener());
    	Btnx_Upgrad.setOnClickListener(new OnBtnxUpgradClickListener());
    	Btnx_Insert.setOnClickListener(new OnBtnxInsertClickListener());
    	Btnx_Update.setOnClickListener(new OnBtnxUpdateClickListener());
    	Btnx_RefxIt.setOnClickListener(new OnBtnxRefxItClickListener());
    	
    } 
    
    class OnBtnxCreateClickListener implements OnClickListener
    {
    	@Override
    	public void onClick(View v)
    	{
    		DataBaseHelper DbHelper = new DataBaseHelper(examplx00004.this,"db_demo",null,1);
    		
    		SQLiteDatabase Db = DbHelper.getReadableDatabase();
    	}
    }
    
    class OnBtnxUpgradClickListener implements OnClickListener
    {
    	@Override
    	public void onClick(View v)
    	{
            DataBaseHelper DbHelper = new DataBaseHelper(examplx00004.this,"db_demo",null,2);
    		SQLiteDatabase Db = DbHelper.getReadableDatabase();    		
    		
    	}
    }
    
    class OnBtnxInsertClickListener implements OnClickListener
    {
    	@Override
    	public void onClick(View v)
    	{
    		DataBaseHelper DbHelper = new DataBaseHelper(examplx00004.this,"db_demo",null,2);
    		SQLiteDatabase Db = DbHelper.getWritableDatabase();
    		
    		ContentValues cv = new ContentValues();
    		cv.put("user_idex", 1);
    		cv.put("user_code", "0001");
    		
    		Db.insert("tbl_user", null, cv);
    	}
    }
    
    class OnBtnxUpdateClickListener implements OnClickListener
    {
    	@Override
    	public void onClick(View v)
    	{
    		DataBaseHelper DbHelper = new DataBaseHelper(examplx00004.this,"db_demo",null,2);
    		SQLiteDatabase Db = DbHelper.getWritableDatabase();
    		
    		ContentValues cv = new ContentValues();
    		cv.put("user_code", "0002");
    		Db.update("tbl_user",cv,"user_idex=?",new String[]{"1"});
    		
    	}
    }
    
    class OnBtnxRefxItClickListener implements OnClickListener
    {
    	@Override
    	public void onClick(View v)
    	{
    		DataBaseHelper DbHelper = new DataBaseHelper(examplx00004.this,"db_demo",null,2);
    		SQLiteDatabase Db = DbHelper.getReadableDatabase();
    		
    		Cursor cr =Db.query("tbl_user",new String[]{"user_idex","user_code"},"",null,null,null,null);     		
    		
    		
    		ArrayList<HashMap<String, String>> ArrayListA = new ArrayList<HashMap<String, String>>();
    		while (cr.moveToNext())
    		{
    			HashMap<String, String> MapA = new HashMap<String, String>();
    			MapA.put("user_idex", cr.getString(cr.getColumnIndex("user_idex")));
    			MapA.put("user_code", cr.getString(cr.getColumnIndex("user_code")));
    			
    			ArrayListA.add(MapA);
    		}	
    		
    		
//    		SimpleAdapter Adapter = new SimpleAdapter(this,
//    				                                  ArrayListA,
//    				                                  R.id.MyListItem,
//    				                                  new String[]{"user_idex","user_code"},
//    				                                  new int[]{R.id.user_idex,R.id.user_code}
//    		                                          );
    		
            SimpleAdapter mSchedule = new SimpleAdapter(examplx00004.this,  
            		ArrayListA,
                    R.layout.listitem,
                    new String[] {"user_idex", "user_code"},   
                    new int[] {R.id.user_idex,R.id.user_code});  
                    List_View.setAdapter(mSchedule);    		
    		
    	}
    }    
    
    
}